package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.api.stock.CqUploadImageAndFileAPI;
import com.steel.yfmall.cloud.assistant.bean.param.APIFileInfoBean;
import com.steel.yfmall.cloud.assistant.utils.kd.CosmicLoginUtil;
import com.steel.yfmall.cloud.components.utils.base.APIListBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @program: CqUploadImageAndFileImpl
 * @author: liuzhijun
 * @date: 2021-04-11 11:34
 **/
@Service
@Slf4j
public class CqUploadImageAndFileImpl implements CqUploadImageAndFileAPI {


    /**
     * 上传图片到苍穹
     *
     * @return com.steel.yfmall.cloud.components.utils.base.APIListBean<com.steel.yfmall.cloud.assistant.bean.param.APIFileInfoBean>
     * @author liuzhijun
     * @date 2021/4/17 11:09
     */
    @Override
    public APIListBean<APIFileInfoBean> uploadImage(List<APIFileInfoBean> apiFileInfoBeanList) {
        APIListBean<APIFileInfoBean> fileInfoBeanAPIListBean = new APIListBean<>();
        List<APIFileInfoBean> apiFileInfoBeans = new ArrayList<>();
        if (apiFileInfoBeanList == null) {
            fileInfoBeanAPIListBean.setData(null);
            fileInfoBeanAPIListBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return fileInfoBeanAPIListBean;
        }

        String result = "";
        String url = "";
        try {
            //获取登录token
            String accessToken = CosmicLoginUtil.getAccessToken(CosmicLoginUtil.GetAPPToken());
            url = DS_COSMIC_IMGAEUPLOAD_URL + "?access_token=" + accessToken;
            String str = "";
            JSONObject obj = new JSONObject();
            obj.put("fileList", apiFileInfoBeanList);
            str = obj.toString();
            result = CosmicLoginUtil.getHttpMsg(str, url);
        } catch (Exception e) {
            log.info("请求url异常:" + url);
        }
        JSONObject jsonObject = JSON.parseObject(result);
        List<APIFileInfoBean> returnFileList = JSONObject.parseArray(jsonObject.get("data").toString(), APIFileInfoBean.class);
        for (APIFileInfoBean item : returnFileList) {
            apiFileInfoBeans.add(item);
        }
        Boolean bool = (Boolean) jsonObject.get("success");
        if (bool) {
            fileInfoBeanAPIListBean.setData(apiFileInfoBeans);
            fileInfoBeanAPIListBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
            return fileInfoBeanAPIListBean;
        } else {
            return fileInfoBeanAPIListBean;
        }
    }


    /**
     * 查询苍穹图片
     *
     * @param apiFileInfoBeanList
     * @return com.steel.yfmall.cloud.components.utils.base.APIListBean<com.steel.yfmall.cloud.assistant.bean.param.APIFileInfoBean>
     * @author liuzhijun
     * @date 2021/4/17 11:09
     */
    @Override
    public APIListBean<APIFileInfoBean> queryImageUrlList(List<APIFileInfoBean> apiFileInfoBeanList) {
        APIListBean<APIFileInfoBean> fileInfoBeanAPIListBean = new APIListBean<>();
        List<APIFileInfoBean> apiFileInfoBeans = new ArrayList<>();
        if (apiFileInfoBeanList == null) {
            fileInfoBeanAPIListBean.setData(null);
            fileInfoBeanAPIListBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return fileInfoBeanAPIListBean;
        }

        String result = "";
        String url = "";
        try {
            //获取登录token
            String accessToken = CosmicLoginUtil.getAccessToken(CosmicLoginUtil.GetAPPToken());
            url = DS_COSMIC_QUERYIMAGE_FILE + "?access_token=" + accessToken;
            String str = "";
            JSONObject obj = new JSONObject();
            obj.put("fileList", apiFileInfoBeanList);
            str = obj.toString();
            result = CosmicLoginUtil.getHttpMsg(str, url);
        } catch (Exception e) {
            log.info("请求url异常:" + url);
        }

        JSONObject jsonObject = JSON.parseObject(result);
        List<APIFileInfoBean> returnFileList = JSONObject.parseArray(jsonObject.get("data").toString(), APIFileInfoBean.class);
        for (APIFileInfoBean item : returnFileList) {
            apiFileInfoBeans.add(item);
        }
        Boolean bool = (Boolean) jsonObject.get("success");
        if (bool) {
            fileInfoBeanAPIListBean.setData(apiFileInfoBeans);
            fileInfoBeanAPIListBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
            return fileInfoBeanAPIListBean;
        } else {
            return fileInfoBeanAPIListBean;
        }
    }

    @Value("${ds_cosmic_imgaeupload_url}")
    private String DS_COSMIC_IMGAEUPLOAD_URL;


    @Value("${ds_cosmic_queryimage_file}")
    private String DS_COSMIC_QUERYIMAGE_FILE;


}
